package cn.com.self.apis.collect.controller;


import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.com.self.apis.collect.entity.CollectionInfo;
import cn.com.self.apis.portal.util.ICustomPage;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import cn.com.self.apis.collect.service.ICollectionInfoService;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangwt
 * @since 2020-05-29
 */
@RestController
@RequestMapping("/collectionInfo")
@Api(tags = "收藏信息类")
public class CollectionInfoController {
	@Autowired
	private ICollectionInfoService collectionInfoService;
	/**新增收藏
     * @Description //TODO
     * @return
     **/
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ApiOperation(value = "新增收藏")
    public ResponseEntity create(@RequestBody CollectionInfo entity, HttpServletRequest request){
        entity.setCreateTime(new Date());
    	CollectionInfo create = collectionInfoService.create(entity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(create));
    }

    /**分页获取托收藏列表
     * @Description //TODO
     * @return
     **/
    @PreAuthorize("hasAuthority('系统管理员')")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取托收藏列表")
    public ResponseEntity getCollectionInfoPage(HttpServletRequest request, Pageable page){
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        ICustomPage<CollectionInfo> pageData = this.collectionInfoService.page(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));

    }


    /**获取单个收藏信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "获取单个收藏信息")
    @RequestMapping(value = {"/load/{id}"}, method = RequestMethod.GET)
    public ResponseEntity loadCollectionInfo(@PathVariable("id") String id,HttpServletRequest request) {
    	CollectionInfo collectionInfo = this.collectionInfoService.load(id);
    	return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(collectionInfo)));

    }


    /**修改收藏信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "修改收藏信息")
    @RequestMapping(value = {"/update/{id}"}, method = RequestMethod.POST)
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody CollectionInfo entity, HttpServletRequest request) {
    	CollectionInfo collectionInfo = this.collectionInfoService.load(id);
    	CopyUtils.copyProperties(entity, collectionInfo);
    	CollectionInfo update = this.collectionInfoService.update(collectionInfo);
    	return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(update)));

    }


    /**删除收藏信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "删除收藏信息")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResponseEntity deletes(@PathVariable("id") String id){
    	this.collectionInfoService.delete(id);
    	return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }
}

